package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.entity.Friend;
import com.zhou.mapper.FriendMapper;
import com.zhou.service.FriendService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.BeanChangeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

import static com.zhou.utils.CommonCast.NO;
import static com.zhou.utils.CommonCast.YES;

/**
 * @Description : 好友关系服务层
 * @Author : zyy
 * @Date: 2024/6/25  11:41
 */
@Service
@Slf4j
public class FriendServiceImpl implements FriendService {
    @Resource
    private FriendMapper friendMapper;

    @Override
    public String add(String uid, String fid) {
        if (uid.equals(fid)) {
            throw new APPApplicationException("不能自己加自己");
        }
        Friend friendOne = friendMapper.selectOne(new LambdaQueryWrapper<Friend>().eq(Friend::getUid, uid)
                .eq(Friend::getFid,fid));
        if (BeanChangeUtils.isNotEmpty(friendOne)) {
            throw new APPApplicationException("已经是好友了不能重复添加");
        }
        Friend friend = new Friend();
        friend.setFriendId(UUID.randomUUID().toString());
        friend.setCreateTime(new Date());
        friend.setFid(fid);
        friend.setUid(uid);
        int insert = friendMapper.insert(friend);
        if (insert > 0) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 是否同意添加好友
     *
     * @param uid     对方用户id
     * @param fid     自己id
     * @param isAgree 是否同意添加好友
     * @return
     */
    @Override
    public String updateAgree(String uid, String fid, Integer isAgree) {
        if (BeanChangeUtils.isEmpty(uid) || BeanChangeUtils.isEmpty(fid) || BeanChangeUtils.isEmpty(isAgree)) {
            throw new APPApplicationException("请传输正确的参数");
        }
        // 查询待通过的好友申请
        Friend byUidAndFid = friendMapper.selectByUidAndFid(uid, fid);
        if (BeanChangeUtils.isEmpty(byUidAndFid)) {
            throw new APPApplicationException("没有待同意的好友申请，请重试");
        }
        byUidAndFid.setIsAgree(isAgree);
        int i = friendMapper.updateById(byUidAndFid);
        if (i <= NO) {
            throw new APPApplicationException("操作好友申请失败");
        }
        //如果等于一就是同意添加好友，没有就不是
        if (isAgree == YES) {
            Friend friend = new Friend();
            friend.setUpdateTime(new Date());
            friend.setFriendId(UUID.randomUUID().toString());
            friend.setIsAgree(isAgree);
            friend.setCreateTime(new Date());
            friend.setFid(fid);
            friend.setUid(uid);
            int insert = friendMapper.insert(friend);
            if (insert > NO) {
                return "添加好友成功";
            }
            return "添加好友失败";
        }else {
            return "拒绝好友申请";
        }

    }

    /**
     * 删除好友
     *
     * @param uid 用户id
     * @param fid 好友id
     * @return
     */
    @Override
    public String deleteById(String uid, String fid) {
        if (BeanChangeUtils.isEmpty(uid) || BeanChangeUtils.isEmpty(fid)) {
            throw new APPApplicationException("请传输正确的参数");
        }
        int delete = friendMapper.delete(new LambdaQueryWrapper<Friend>().eq(Friend::getUid, uid)
                .eq(Friend::getFid, fid));
        if (delete > NO) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 拉黑好友
     *
     * @param uid       用户id
     * @param fid       好友id
     * @param blackLike 是否拉黑 0否 1是
     * @return
     */
    @Override
    public String updateBlock(String uid, String fid, Integer blackLike) {
        if (BeanChangeUtils.isEmpty(uid) || BeanChangeUtils.isEmpty(fid) || BeanChangeUtils.isEmpty(blackLike)) {
            throw new APPApplicationException("请传输正确的参数");
        }
        Integer i = friendMapper.updateByBlcakLike(uid, fid, blackLike);
        if (i > NO) {
            return "拉黑成功";
        }
        return "拉黑失败";
    }
}
